using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Data.SQLite;
using Common;

namespace Webgame.Access
{
    partial class PlayerScore
    {
        static object addLock = new object();

        string insertSQL = "insert into PlayerScore([PlayerID],[MapID],[Nutrient],[Eat],[Virus])  values(@PlayerID,@MapID,@Nutrient,@Eat,@Virus)";
        /// <summary>
        /// 添加一条新的记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Webgame.Entity.PlayerScore model)
        {
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("@PlayerID", model.PlayerID),
                new SQLiteParameter("@MapID", model.MapID),
                new SQLiteParameter("@Nutrient", model.Nutrient),
                new SQLiteParameter("@Eat", model.Eat),
                new SQLiteParameter("@Virus", model.Virus),};
            lock (addLock)
            {
                int row = SQLiteHelper.ExecuteNonQuery(insertSQL, parameters);

                DataSet ds = SQLiteHelper.ExecuteDataset("select max(id) from player");
                int id = (int)(Int64)ds.Tables[0].Rows[0][0];
                model.ID = id;
            }

            return model.ID;
        }

        string updateSQL = "update PlayerScore Set [PlayerID]=@PlayerID,[MapID]=@MapID,[Nutrient]=@Nutrient,[Eat]=@Eat,[Virus]=@Virus where [ID]=@ID";        /// <summary>
        /// 更新一个对象
        /// </summary>
        /// <param name="model"></param>
        public void Update(Webgame.Entity.PlayerScore model)
        {
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("@ID", model.ID),
                new SQLiteParameter("@PlayerID", model.PlayerID),
                new SQLiteParameter("@MapID", model.MapID),
                new SQLiteParameter("@Nutrient", model.Nutrient),
                new SQLiteParameter("@Eat", model.Eat),
                new SQLiteParameter("@Virus", model.Virus),};
            SQLiteHelper.ExecuteNonQuery(updateSQL, parameters);
        }

        string deleteSQL = "delete from PlayerScore where [ID]=@ID";

        /// <summary>
        /// 删除一个对象
        /// </summary>
        /// <param name="model"></param>
        public void Delete(Webgame.Entity.PlayerScore model)
        {
            Delete(model.ID);
        }

        /// <summary>
        /// 通过关键字段ID删除一条记录
        /// </summary>
        /// <param name="ID"></param>
        public void Delete(int ID)
        {
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("@ID", ID)};

            SQLiteHelper.ExecuteNonQuery(deleteSQL, parameters);

        }

        string selectSQL = "select * from PlayerScore";

        /// <summary>
        /// 根据关键字段ID获得一条数据记录
        /// 如果不存在记录则返回NULL
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public Webgame.Entity.PlayerScore GetPlayerScoreByID(int ID)
        {
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("@ID", ID)};

            string sql = selectSQL + " where ID=@ID";

            IDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters);

            return GetSingle(reader);
        }

        /// <summary>
        /// 从返回的记录里获得第一条数据
        /// 如果没有数据，则返回NULL
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        private static Webgame.Entity.PlayerScore GetSingle(IDataReader reader)
        {
            Webgame.Entity.PlayerScore ret = null;

            try
            {
                if (reader.Read())
                {
                    ret = GetModel(reader);
                }
            }
            finally
            {
                reader.Close();
            }

            return ret;
        }

        /// <summary>
        /// 获得数据库表里所有数据
        /// </summary>
        /// <returns></returns>
        public List<Webgame.Entity.PlayerScore> GetPlayerScoreAll()
        {
            IDataReader reader = SQLiteHelper.ExecuteReader(selectSQL);

            return GetMultiEntity(reader);        
        }

        /// <summary>
        /// 从DataReader里获得多条记录
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        private static List<Webgame.Entity.PlayerScore> GetMultiEntity(IDataReader reader)
        {
            List<Webgame.Entity.PlayerScore> ret = new List<Webgame.Entity.PlayerScore>();

            try
            {
                while (reader.Read())
                {
                    ret.Add(GetModel(reader));
                }
            }
            finally
            {
                reader.Close();
            }

            return ret;
        }

        private static Webgame.Entity.PlayerScore GetModel(IDataReader reader)
        {
            Webgame.Entity.PlayerScore ret = new Webgame.Entity.PlayerScore();

            ret.ID = (int)(Int64)reader["ID"];
            ret.PlayerID = (int)(Int64)reader["PlayerID"];
            ret.MapID = (int)(Int64)reader["MapID"];
            ret.Nutrient = (int)(Int64)reader["Nutrient"];
            ret.Eat = (int)(Int64)reader["Eat"];
            ret.Virus = (int)(Int64)reader["Virus"];

            return ret;
        }
        static PlayerScore instance = null;

        public static PlayerScore Instance
        {
            get
            {
                if (instance == null)
                    instance = new PlayerScore();
                return PlayerScore.instance;
            }
        }

    }
}

